package com.sample.base.ui.fragment.recyclerview

import android.os.Bundle
import android.text.TextUtils
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.loyal.kit.TimeUtil
import com.sample.base.adapter.RecycleViewAdapter
import com.sample.base.databinding.FragmentRecyclerListBinding
import com.sample.base.ui.fragment.BaseFragment

class RecyclerListFragment : BaseFragment() {
    private var _binding: FragmentRecyclerListBinding? = null
    private val binding get() = _binding!!

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        _binding = FragmentRecyclerListBinding.inflate(inflater, container, false)
        return binding.root
    }

    override fun afterOnCreate() {
        val strList = listOf("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11")
        val recycleAdapter = RecycleViewAdapter(strList)
        binding.recycle.adapter = recycleAdapter
        recycleAdapter.setOnItemClickListener { adapter, _, position, _ ->
            Log.e("itemClick", "position=$position,itemValue=${adapter.getItem(position)}")
        }

        binding.insertHead.setOnClickListener {
            val time = TimeUtil.getDateTime()
            recycleAdapter.insertAtPosition(0, "头部+1($time)")
        }
        binding.insertEnd.setOnClickListener {
            val time = TimeUtil.getDateTime()
            recycleAdapter.insert("尾部+1($time)")
        }
        binding.insertHeadMore.setOnClickListener {
            val insertList = listOf("头部+ N1", "头部+ N2", "头部+ N3", "头部+ N4")
            recycleAdapter.insertRange(0, insertList)
        }
        binding.insertEndMore.setOnClickListener {
            val insertList = listOf("尾部+ N1", "尾部+ N2", "尾部+ N3", "尾部+ N4")
            recycleAdapter.insertRange(recycleAdapter.itemCount, insertList)
        }

        //删除
        binding.deleteHead.setOnClickListener {
            recycleAdapter.remove(0)
        }
        binding.deleteEnd.setOnClickListener {
            val lastPosition = recycleAdapter.itemCount
            recycleAdapter.remove(lastPosition - 1)
        }
        binding.deleteHeadMore.setOnClickListener {
            recycleAdapter.removeRange(0, 4)
        }
        binding.deleteEndMore.setOnClickListener {
            if (recycleAdapter.isEmpty)
                return@setOnClickListener

            val maxCount = recycleAdapter.itemCount
            val deleteCount = 4
            val position = maxCount - deleteCount
            if (position < 0) {
                return@setOnClickListener
            }
            recycleAdapter.removeRange(position, deleteCount)
        }
        binding.empty.setOnClickListener {
            recycleAdapter.notifyEmpty()
        }

        binding.insertPosition.setOnClickListener {
            val position = binding.editPosition.text?.trim().toString()
            if (TextUtils.isEmpty(position))
                return@setOnClickListener
            val intPosition = position.toInt()
            if (intPosition > recycleAdapter.itemCount)
                return@setOnClickListener
            recycleAdapter.insertAtPosition(position.toInt(), "INSERT-POSITION")
        }
        binding.deletePosition.setOnClickListener {
            val position = binding.editPosition.text?.trim().toString()
            if (TextUtils.isEmpty(position))
                return@setOnClickListener
            val intPosition = position.toInt()
            if (intPosition > recycleAdapter.itemCount - 1)
                return@setOnClickListener
            recycleAdapter.remove(position.toInt())
        }
    }

    companion object {
        private const val ARG_PARAM1 = "param1"
        fun newInstance(param1: String? = ""): RecyclerListFragment {
            val fragment = RecyclerListFragment()
            val args = Bundle()
            args.putString(ARG_PARAM1, param1)
            fragment.arguments = args
            return fragment
        }
    }

    override fun unbind() {
        super.unbind()
        _binding = null
    }
}